
//
//  FiveViewController.m
//  YIChuSeller
//
//  Created by xsj on 2018/11/24.
//  Copyright © 2018年 xiangshengjun. All rights reserved.
//

#import "GXCancelController.h"
#import "GXCancelViewCell.h"
#import "OrderAndUploadPhotoMananger.h"
#import "GXCommonOrderDetailController.h"
@interface GXCancelController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong)UITableView *cancelTableView;
@property (nonatomic,strong)NSMutableArray *dataArray;
@property (nonatomic,assign) int addStartNumber;
@end
static NSString *const cancelViewCell=@"GXCancelViewCell";
@implementation GXCancelController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatAllView];
}
- (void)creatAllView
{
    [self.view addSubview:self.cancelTableView];
    self.cancelTableView.mj_header =[MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshData)];
    self.cancelTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
      [self.cancelTableView.mj_header beginRefreshing];
}

- (NSMutableArray *)dataArray
{
    if (_dataArray == nil) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
#pragma mark - loadData
- (void)refreshData
{
    _addStartNumber = 0;
    [self requestDataSourceWithStartIndex:_addStartNumber];
}
- (void)loadMoreData
{
    [self requestDataSourceWithStartIndex:++_addStartNumber];
}
- (void)requestDataSourceWithStartIndex:(int)startIndex
{
    if (startIndex == 0) {
        [self.dataArray removeAllObjects];
    }
    NSString *pageStr = [NSString stringWithFormat:@"%d",startIndex];
    [OrderAndUploadPhotoMananger selectShopOrderWithStoreID:StoreId state:@"2" page:pageStr success:^(id responseObject) {
        if (responseObject) {
            [self.dataArray addObjectsFromArray:responseObject];
            [self.cancelTableView reloadData];
        }
        [self.cancelTableView.mj_header endRefreshing];
        [self.cancelTableView.mj_footer endRefreshing];
    } fail:^(id responseObject) {
        [self.cancelTableView.mj_header endRefreshing];
        [self.cancelTableView.mj_footer endRefreshing];
    }];
}
#pragma mark -- 创建uitableview
- (UITableView *)cancelTableView
{
    if (_cancelTableView == nil ) {
        _cancelTableView =[[UITableView alloc] initWithFrame:CGRectMake(0,0,kUIScreenWidth,kUIScreenHeight-Height_TabBar-Height_StatusBar-43) style:UITableViewStyleGrouped];
        _cancelTableView.delegate=self;
        _cancelTableView.dataSource=self;
        _cancelTableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, CGFLOAT_MIN)];
        _cancelTableView.showsHorizontalScrollIndicator=NO;
        _cancelTableView.showsVerticalScrollIndicator=NO;
        _cancelTableView.sectionHeaderHeight = 15;
        _cancelTableView.sectionFooterHeight = 0.01f;
        _cancelTableView.separatorStyle=UITableViewCellSeparatorStyleNone;
        _cancelTableView.backgroundColor =kGlobalbgColor;
        [_cancelTableView registerNib:[UINib nibWithNibName:@"GXCancelViewCell" bundle:nil] forCellReuseIdentifier:cancelViewCell];
        if (@available(iOS 11.0, *)) {
            _cancelTableView.estimatedRowHeight = 0;
            _cancelTableView.estimatedSectionFooterHeight = 0;
            _cancelTableView.estimatedSectionHeaderHeight=0;
            _cancelTableView.contentInsetAdjustmentBehavior= UIScrollViewContentInsetAdjustmentNever;
        }
    }
    return _cancelTableView;
}

#pragma mark ---UITableViewDataSource UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    GXCancelViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cancelViewCell];
    OrderMessageModel * model = self.dataArray[indexPath.section];
    if (model) {
        cell.model = model;
    }
    return cell;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 236;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView * headerView = [[UIView alloc]init];
    headerView.backgroundColor = [UIColor clearColor];
    return headerView;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    GXCommonOrderDetailController *detailVC = [[GXCommonOrderDetailController alloc]init];
    OrderMessageModel *model = self.dataArray[indexPath.section];
    if (model) {
        detailVC.onumber = [NSString stringWithFormat:@"%@",model.oNumber];
        [self.navigationController pushViewController:detailVC animated:YES];
    }
}

@end
